﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using UTSDAL.LINQSQL.Model;
using UTSDAL.LINQSQL.DAO;


namespace UTS.Controls
{
    [
        DefaultProperty("Text"),
        ToolboxData("<{0}:LecturerDropDownList runat=\"server\"> </{0}:LecturerDropDownList>")
    ]
    public class LecturerDropDownList : System.Web.UI.WebControls.DropDownList
    {

        private LecturerDAO lecturerDAO = new LecturerDAO();

        private void init()
        {

            this.DataSource = lecturerDAO.selectAll();
            this.DataBind();
            DropDownUtils.addDefaultLabel(this, "Select a Lecturer");
            this.DataTextField = "LecturerName";
            this.DataValueField = "LecturerId";
        }

        // this method bypasses non post back requests by ajax
        public void reload()
        {
            init();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                init();
            }
        }


        private Lecturer lecturer;

        public Lecturer Lecturer
        {
            get
            {
                if (StringUtils.isValidGuid(this.SelectedValue.ToString()))
                {
                    lecturer = lecturerDAO.findById(new Guid(this.SelectedValue.ToString()));
                }
                return Lecturer;
            }
            set
            {
                lecturer = value;

                if (lecturer != null)
                {
                    this.SelectedValue = lecturer.LecturerId.ToString();
                }
            }
        }


    }
}